﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SmoothControls
{
	public partial class SideMenuItem : UserControl
	{
		public SideMenuItem()
		{
			InitializeComponent();
		}

		public SideMenuItem(String text, Side side)
		{
			InitializeComponent();
			this.Text = text;
			SideSelector = side;
		}

		public String Text { get; set; }
		public Side SideSelector{get; set;}
		public enum Side
		{
			Left = 0,
			Right = 1
		}

		protected override void OnPaintBackground( PaintEventArgs e )
		{
			//Do Nothing
		}

		protected override void OnPaint( PaintEventArgs e )
		{
			using ( Image flickerBuffer = new Bitmap( this.Width, this.Height ) )
			{
				using ( Graphics flickerGraphics = Graphics.FromImage( flickerBuffer ) )
				{
					flickerGraphics.Clear(Color.Black);
					Rectangle rect = e.ClipRectangle;
					rect.Size = new Size(rect.Width-1, rect.Height-1);
					flickerGraphics.FillRectangle(new SolidBrush(Color.Black), rect);
					flickerGraphics.DrawRectangle(new Pen(Color.Orange), rect);
					if ( SideSelector == Side.Left )
					{
						SizeF stringSize = flickerGraphics.MeasureString( Text, new Font( FontFamily.GenericSansSerif, 11f, FontStyle.Bold ) );
						Rectangle rectString = new Rectangle();
						rectString.Size = new Size( (int)stringSize.Width + 6, (int)stringSize.Height );
						rectString.Location = new Point( rect.Right - rectString.Width, (int)( rect.Height / 2 ) - (int)( rectString.Height / 2 ) );
						flickerGraphics.DrawString( Text, new Font( FontFamily.GenericSansSerif, 11f, FontStyle.Bold ), new SolidBrush( Color.White ), rectString );
					}
					else
					{
						SizeF stringSize = flickerGraphics.MeasureString( Text, new Font( FontFamily.GenericSansSerif, 11f, FontStyle.Bold ) );
						Rectangle rectString = new Rectangle();
						rectString.Size = new Size( (int)stringSize.Width + 6, (int)stringSize.Height );
						rectString.Location = new Point( rect.Left+6, (int)( rect.Height / 2 ) - (int)( rectString.Height / 2 ) );
						flickerGraphics.DrawString( Text, new Font( FontFamily.GenericSansSerif, 11f, FontStyle.Bold ), new SolidBrush( Color.White ), rectString );
					}
					e.Graphics.DrawImage( flickerBuffer, 0, 0 );
				}
			}
		}
	}
}
